package com.example.smartall.fragment.JobHunting;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.smartall.R;
import com.example.smartall.adapter.PervasiveAdapter;
import com.example.smartall.api.Api;
import com.example.smartall.api.ApiConfig;
import com.example.smartall.api.NetCallBack;
import com.example.smartall.entity.JobDeliverHistoryBean;
import com.example.smartall.fragment.base.BaseFragment;
import com.example.smartall.ui.LoginActivity;
import com.example.smartall.utils.Constant;
import com.example.smartall.utils.SPUtils;
import com.example.smartall.view.SListView;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

 /**
  * @Author:         abbas
  * @CreateDate:     2022/2/6
  * @Description:    2.投递记录页面，列表显示已经投简历的公司，信息包括岗位名称、公司名称、薪资和投递简历时间。
  */

public class DeliverHistoryFragment extends BaseFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;

    //一千多条数据，你不分页也得分页
    private final int PAGE_SIZE = 12;
    private int PAGE_NUM = 1;

    private SListView lvDeliverHistory;
    private Button btLoadMore;

    private MHandler handler;
    private List<JobDeliverHistoryBean.RowsBean> deliverHistoryBeans;
    private DeliverHistoryListAdapter listAdapter;

    public DeliverHistoryFragment() {
    }

    public static DeliverHistoryFragment newInstance(String param1, String param2) {
        DeliverHistoryFragment fragment = new DeliverHistoryFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        handler = new MHandler();

        View view = inflater.inflate(R.layout.fragment_deliver_history, container, false);
        init(view);
        initEvent();
        getDeliverHistory(PAGE_NUM);
        return view;
    }

    private void init(View view) {
        lvDeliverHistory = (SListView) view.findViewById(R.id.lv_deliverHistory);
        btLoadMore = (Button) view.findViewById(R.id.bt_loadMore);

    }

    private void initEvent() {
        btLoadMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listAdapter == null) {
                    showToast("加载中，请稍等...");
                    return;
                }
                ++PAGE_NUM;
                getDeliverHistory(PAGE_NUM);
            }
        });
    }

     /**
      * 获取投递历史
      * @param pageNum 当前分页页数
      */
    private void getDeliverHistory(int pageNum) {
        String params = "?pageSize=" + PAGE_SIZE + "&pageNum=" + pageNum;
        Api.config(ApiConfig.JOB_DELIVER_HISTORY,null).getRequest(getActivity(), new NetCallBack() {
            @Override
            public void onSuccess(String res) {
                JobDeliverHistoryBean bean = new Gson().fromJson(res, JobDeliverHistoryBean.class);
                if (bean != null && bean.getCode() == 200) {
                    if (deliverHistoryBeans == null) {
                        deliverHistoryBeans = new ArrayList<>();
                    }
                    deliverHistoryBeans.addAll(bean.getRows());

                    handler.sendEmptyMessage(0x01);
                }
            }

            @Override
            public void onFailure(Exception e) {

            }
        },params);
    }

    class MHandler extends Handler {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0x01:
                    if (listAdapter == null) {
                        listAdapter = new DeliverHistoryListAdapter(deliverHistoryBeans);
                        lvDeliverHistory.setAdapter(listAdapter);
                    } else {
                        listAdapter.setData(deliverHistoryBeans);
                    }
                    break;
            }
        }
    }

    class DeliverHistoryListAdapter extends PervasiveAdapter<JobDeliverHistoryBean.RowsBean> {

        public DeliverHistoryListAdapter(List<JobDeliverHistoryBean.RowsBean> datas) {
            super(datas);
        }

        @Override
        protected void setUI(ViewHolder holder, int position, Context context) {
            ImageView img = (ImageView) holder.getItemView(R.id.img);
            img.setVisibility(View.GONE);
            TextView title = (TextView) holder.getItemView(R.id.title);
            TextView subtitle = (TextView) holder.getItemView(R.id.subtitle);
            TextView content = (TextView) holder.getItemView(R.id.content);

            title.setText(deliverHistoryBeans.get(position).getPostName());
            subtitle.setText(deliverHistoryBeans.get(position).getCompanyName());
            content.setText("薪资：" + deliverHistoryBeans.get(position).getMoney() +
                    "\n" + deliverHistoryBeans.get(position).getSatrTime());
        }

        @Override
        protected int getLayoutId() {
            return R.layout.common_list_item;
        }
    }
}